package com.pk.platform.business.charge.service.impl;

import java.util.List;

import com.pk.platform.business.charge.dao.IChargeTempDao;
import com.pk.platform.business.charge.service.IChargeTempService;
import com.pk.platform.business.charge.vo.ChargeTempVO;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.domain.charge.ChargeTemp;
import com.pk.platform.domain.charge.TempIndexRef;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class ChargeTempServiceImpl implements IChargeTempService {

	private IChargeTempDao chargeTempDao;
	
	public ListPage queryChargeTemp(ChargeTempVO ctvo, Pager pager)
	{
		return chargeTempDao.queryChargeTemp(ctvo, pager);
	}
	
	public void addChargeTemp(ChargeTemp ct, String[] indexId, String sortStr, String amountStr, String quantityStr)
	{
		chargeTempDao.add(ct);
		this.addTempIndexRef(ct, indexId, sortStr, amountStr, quantityStr);
		chargeTempDao.addOperateLog("新增费用模板", Constant.SUCCESSFUL, ct.getKindergarten());
	}
	
	public void updateChargeTemp(ChargeTemp ct, String[] indexId, String sortStr, String amountStr, String quantityStr)
	{
		ChargeTemp chargeTemp = this.queryChargeTempById(ct.getId());
		//删除关系
		chargeTemp.getChargeIndexSet().clear();
		chargeTempDao.getHibernateSession().flush();
		
		chargeTemp.setTempName(ct.getTempName());
		chargeTemp.setDescription(ct.getDescription());
		chargeTemp.setCla(ct.getCla());
		//添加关系
		this.addTempIndexRef(chargeTemp, indexId, sortStr, amountStr, quantityStr);
		chargeTempDao.addOperateLog("修改费用模板", Constant.SUCCESSFUL, chargeTemp.getKindergarten());
	}
	
	public void delChargeTemp(String id)
	{
		ChargeTemp ct = this.queryChargeTempById(id);
		chargeTempDao.addOperateLog("删除费用模板", Constant.SUCCESSFUL, ct.getKindergarten());
		//删除关系
		ct.getChargeIndexSet().clear();
		chargeTempDao.getHibernateSession().flush();
		chargeTempDao.delete(ChargeTemp.class, id);
	}
	
	public ChargeTemp queryChargeTempById(String id)
	{
		return chargeTempDao.get(ChargeTemp.class, id);
	}
	
	//添加模板与指标关系
	private void addTempIndexRef(ChargeTemp ct, String[] indexId, String sortStr, String amountStr, String quantityStr)
	{
		if(indexId != null)
		{
			String[] sort = sortStr.split(",");
			String[] amount = amountStr.split(",");
			String[] quantity = quantityStr.split(",");
			int len = indexId.length;
			int actualLen = len - 1; //因为要除去临时添加的
			for(int i = 0; i < actualLen; i++)
			{
				int quantityInt = Integer.parseInt(quantity[i]);
				for(int j = 0; j < quantityInt; j++) //数量
				{
					ChargeIndex ci = new ChargeIndex(indexId[i]);
					TempIndexRef tir = new TempIndexRef();
					tir.setChargeTemp(ct);
					tir.setChargeIndex(ci);
					tir.setSort(Integer.parseInt(sort[i]));
					tir.setAmount(Double.parseDouble(amount[i]));
					tir.setTeam(i);
					chargeTempDao.add(tir);
				}
			}
		}
	}
	
	public List<TempIndexRef> queryTirList(String tempId)
	{
		return chargeTempDao.queryTirList(tempId);
	}
	
	

	public IChargeTempDao getChargeTempDao() {
		return chargeTempDao;
	}

	public void setChargeTempDao(IChargeTempDao chargeTempDao) {
		this.chargeTempDao = chargeTempDao;
	}

}
